package com.hdc.lw.service;

import com.hdc.lw.mapper.CategoryMapper;
import com.hdc.lw.po.Category;
import com.hdc.lw.po.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService{

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询商品分类列表
     * @return
     */
    @Override
    public Object findAllCategory() {
        Response response = new Response();
        List categorylist = categoryMapper.findAllCategory();
        if(categorylist != null){
            response = new Response(Response.COMMON_SUCCESS,"查询成功",categorylist);
        } else {
            response = new Response(Response.COMMON_FAIL, "查询失败", null);
        }
        return response;
    }

    /**
     * 添加商品分类
     * @param category
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)  // 增删改请求需要控制事务
    @Override
    public Object addCategory(Category category) {
        Response response = new Response();
        Integer row = categoryMapper.addCategory(category);
        if(row>0){
            response =new Response(Response.COMMON_SUCCESS,"添加成功!",null);
        } else {
            response = new Response(Response.COMMON_FAIL, "添加失败", null);
        }
        return response;
    }

    /**
     * 删除商品分类
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)  // 增删改请求需要控制事务
    @Override
    public Object deleteCategory(Integer id) {
        Response response = new Response();
        Integer row = categoryMapper.deleteCategory(id);
        if(row>0){
            response =new Response(Response.COMMON_SUCCESS,"删除成功!",null);
        } else {
            response = new Response(Response.COMMON_FAIL, "删除失败", null);
        }
        return response;
    }

    /**
     * 更新商品分类
     * @param category
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)  // 增删改请求需要控制事务
    @Override
    public Object updateCategory(Category category) {
        Response response = new Response();
        Integer row = categoryMapper.updateCategory(category);
        if(row>0){
            response =new Response(Response.COMMON_SUCCESS,"更新成功!",null);
        } else {
            response = new Response(Response.COMMON_FAIL, "更新失败", null);
        }
        return response;
    }
}
